package com.qdxwx.manage.controller.manager;


import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.manager.ManagerOnlineService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Logical;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.enums.OnlineStatus;
import com.qdxwx.models.manager.ManagerOnline;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 在线用户监控
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/monitor/online")
public class ManagerOnlineController extends BaseController
{
    private String prefix = "monitor/online";

    @Autowired
    private ManagerOnlineService userOnlineService;

    //@Autowired
    //private OnlineSessionDAO onlineSessionDAO;

    @Permissions("monitor:online:list")
    @GetMapping("/list.html")
    public String online()
    {
        return prefix + "/online";
    }

    @Permissions("monitor:online:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, ManagerOnline userOnline)
    {
        List<ManagerOnline> list = userOnlineService.getList(userOnline, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(userOnlineService.count(userOnline));
        return rspData;
    }

    @Permissions(value = { "monitor:online:batchForceLogout", "monitor:online:forceLogout" }, logical = Logical.OR)
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @PostMapping("/batchForceLogout.json")
    @ResponseBody
    public AjaxResult batchForceLogout(String ids)
    {
        for (String sessionId : TKConvert.toStrArray(ids))
        {
            ManagerOnline online = userOnlineService.getById(sessionId);
            if (online == null)
            {
                return error("用户已下线");
            }
            //OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
            //if (onlineSession == null)
            //{
            //    return error("用户已下线");
            //}
            //if (sessionId.equals(SessionUtils.getSessionId()))
            //{
            //    return error("当前登陆用户无法强退");
            //}
            //onlineSession.setStatus(OnlineStatus.off_line);
            //onlineSessionDAO.update(onlineSession);
            online.setStatus(OnlineStatus.off_line);
            userOnlineService.save(online);
        }
        return success();
    }
}
